package com.pattern.objectcreator.singleton;

/**
 * @author yangguanzan
 * @date 3/15/22
 * @description
 */
public class SingletonLazyLoading {

    private static SingletonLazyLoading INSTACNCE;

    private SingletonLazyLoading(){
        //禁止new实例化
    }


    /**
     * 双重校验锁实现懒加载，保证线程安全
     */
    public static SingletonLazyLoading instanceDCL() {
        //双重校验锁
        if(null==INSTACNCE){
            synchronized (SingletonLazyLoading.class){
                if(null==INSTACNCE){
                    INSTACNCE = new SingletonLazyLoading();
                }
            }
        }
        return INSTACNCE;
    }

    private static class SingletonInstance {
        private static final SingletonLazyLoading INSTACNCE = new SingletonLazyLoading();
    }

    /**
     * 静态内部类实现懒加载，保证线程安全
     */
    public static SingletonLazyLoading instance() {
        INSTACNCE = SingletonInstance.INSTACNCE;
        return SingletonInstance.INSTACNCE;
    }
}
